package org.jash.ting13.book

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import org.jash.ting13.R
import org.jash.ting13.BR
import org.jash.ting13.entity.Chapter
import org.jash.utils.CommonViewHolder

class ChapterAdapter:PagingDataAdapter<Chapter, CommonViewHolder>(ChapterCallback) {
    override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
        holder.binding.setVariable(BR.chapter, getItem(position))
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder =
        CommonViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_chapter, parent, false))
}
private object ChapterCallback:DiffUtil.ItemCallback<Chapter>() {
    override fun areItemsTheSame(oldItem: Chapter, newItem: Chapter): Boolean = oldItem.url == newItem.url

    override fun areContentsTheSame(oldItem: Chapter, newItem: Chapter): Boolean = oldItem == newItem

}